package cn.hesion.leetcode;
/**
 * ClassName: CountKDifference <br/>
 * Description: <br/>
 * date: 2022/5/22 7:27<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.HashMap;

/**
 *2006. 差的绝对值为 K 的数对数目
 * @author: hesion
 * @create: 2022-05-22 07:27
 **/
public class CountKDifference {
    public int countKDifference(int[] nums, int k) {
        int ans = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            ans += map.getOrDefault(nums[i] - k, 0) + map.getOrDefault(nums[i] + k, 0);
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        return ans;
    }
    public static void main(String[] args) {
        System.out.println(new CountKDifference().countKDifference(new int[]{1, 2, 2, 1}, 1));
    }
}
